//
//  Flipper_L_Obstacle.m
//  StarBugs
//
//  Created by ?? ? on 4/16/12.
//  Copyright 2012 antisword@playorca.com. All rights reserved.
//

#include "Object/Flipper_L_Obstacle.h"
#include "Util/ImageManager.h"

using namespace cocos2d;


Flipper_L_Obstacle::Flipper_L_Obstacle()
{

    mainSprite = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "flipper");
    mainSprite->setScaleX(-1);
    mainSprite->setAnchorPoint(ccp(55 / 66.f, 0.5));
    
    addChild(mainSprite);
    
    objectType = objectType_flipper_L;
}

void Flipper_L_Obstacle::setBody(b2World *world)
{
    mainSprite->setPosition(position);
    b2CircleShape circ;
    
    b2BodyDef bd;
    b2RevoluteJointDef jd;
    b2FixtureDef fixtureDef;
    
    bd.type = b2_staticBody;
    
    // Head
    circ.m_radius = 2 / PTM_RATIO;
    fixtureDef.shape = &circ;
    bd.position.Set((position.x) / PTM_RATIO, position.y / PTM_RATIO);
    head = world->CreateBody(&bd);
    head->CreateFixture(&fixtureDef);
    
    
    //    b2ChainShape poly;
    b2PolygonShape poly;
    b2Vec2 vertices[] = 
    {
        b2Vec2(-3/PTM_RATIO, 9/PTM_RATIO), b2Vec2(-11/PTM_RATIO, 0/PTM_RATIO), b2Vec2(-5/PTM_RATIO, -8/PTM_RATIO), b2Vec2(51/PTM_RATIO, -6/PTM_RATIO), b2Vec2(55/PTM_RATIO, -1/PTM_RATIO),
        b2Vec2(52/PTM_RATIO, 3/PTM_RATIO),
    };
    
    poly.Set(vertices, 6);
    fixtureDef.shape = &poly;
    bd.type = b2_dynamicBody;
    bd.userData = mainSprite;
    
    fixtureDef.density = 10.0;
//    fixtureDef.friction = 1;
    fixtureDef.restitution = 0.5;
    
    bd.position.Set((position.x) / PTM_RATIO, (position.y)  / PTM_RATIO);
    stick = world->CreateBody(&bd);
    stick->CreateFixture(&fixtureDef);
    
    
    //    jd.enableLimit = true;
    jd.Initialize(stick, head, b2Vec2((position.x) / PTM_RATIO, (position.y) / PTM_RATIO));
    jd.motorSpeed = 0.0f;
    jd.maxMotorTorque = 2000.0f;
    jd.enableMotor = true;
    jd.lowerAngle = -50.0f * b2_pi / 180.f;
    jd.upperAngle = 0.0f * b2_pi / 180.f;
    jd.enableLimit = true;
    
    
    jt = (b2RevoluteJoint*)world->CreateJoint(&jd);
    
    head->SetTransform(b2Vec2(position.x / PTM_RATIO, (position.y) / PTM_RATIO), CC_DEGREES_TO_RADIANS(bodyAngle));
    stick->SetTransform(b2Vec2(position.x / PTM_RATIO, (position.y) / PTM_RATIO), CC_DEGREES_TO_RADIANS(bodyAngle));
}

void Flipper_L_Obstacle::removeBody(b2World *_world)
{
    _world->DestroyJoint(jt);
    _world->DestroyBody(head);
    _world->DestroyBody(stick);
}



void Flipper_L_Obstacle::setBodyAngle(bool isInv)
{
    if(isInv)
        bodyAngle += 10;
    else
        bodyAngle -= 10;
    
    head->SetTransform(b2Vec2(position.x / PTM_RATIO, (position.y) / PTM_RATIO), CC_DEGREES_TO_RADIANS(bodyAngle));
    stick->SetTransform(b2Vec2(position.x / PTM_RATIO, (position.y) / PTM_RATIO), CC_DEGREES_TO_RADIANS(bodyAngle));
    
}

void Flipper_L_Obstacle::setMove(cocos2d::CCPoint pt)
{
    position = pt;
    head->SetTransform(b2Vec2(position.x / PTM_RATIO, (position.y) / PTM_RATIO), CC_DEGREES_TO_RADIANS(bodyAngle));
    stick->SetTransform(b2Vec2(position.x / PTM_RATIO, (position.y) / PTM_RATIO), CC_DEGREES_TO_RADIANS(bodyAngle));
}

void Flipper_L_Obstacle::onFlipper()
{
    jt->SetMotorSpeed(-25.0f);
}

void Flipper_L_Obstacle::offFlipper()
{
    jt->SetMotorSpeed(10.0f);
}

